package com.FH.GET_IPInfo;

import com.FH.util.ResultDTO;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * Created by Administrator on 2016/10/17.
 */
@Path("/GETPCINFO")
@Produces(MediaType.APPLICATION_JSON)
@RequestScoped
public class SignInfoResource  {
    @EJB
    private signInfoDAO signInfoDAO;
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String GETPCINFO(@Context HttpServletRequest request){
        String IP=getIpAddr(request);
        return IP;
    }
    public  String getIpAddr(HttpServletRequest request)  {
        String ip  =  request.getHeader( " x-forwarded-for " );
        if (ip  ==   null   ||  ip.length()  ==   0   ||   " unknown " .equalsIgnoreCase(ip))  {
            ip  =  request.getHeader( " Proxy-Client-IP " );
        }
        if (ip  ==   null   ||  ip.length()  ==   0   ||   " unknown " .equalsIgnoreCase(ip))  {
            ip  =  request.getHeader( " WL-Proxy-Client-IP " );
        }
        if (ip  ==   null   ||  ip.length()  ==   0   ||   " unknown " .equalsIgnoreCase(ip))  {
            ip  =  request.getRemoteAddr();
        }
        return  ip;
    }


    /**
     * 按条件查询列表
     * @param search
     * @return
     */
    @GET
    @Path("/{search}")
    public ResultDTO getDepartmentInfo(@PathParam("search") String search){

        return signInfoDAO.getSignInfo(search);
    }

    /**
     * 按条件查询签到信息列表
     * @param search
     * @return
     */
    @GET
    @Path("/signInfo/{search}")
    public ResultDTO getSignInfoList(@PathParam("search") String search){

        return signInfoDAO.getSignInfoList(search);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public ResultDTO addDepartmentInfo(signInfoPullDTO signinfopullDTO, @Context HttpServletRequest request){
        String IP=getIpAddr(request);
        signinfopullDTO.setIP(IP);
        return signInfoDAO.addSignInfo(signinfopullDTO);
    }

    /**
     * 删除方法
     * @param
     * @return
     */
    @PUT
    @Path("/delete")
    @Consumes(MediaType.APPLICATION_JSON)
    public ResultDTO deleteDepartmentTypeInfo(signInfoPullDTO signinfopullDTO){

        return signInfoDAO.deleteSignInfo(signinfopullDTO);
    }

    @GET
    @Path("/download/{search}")
    public ResultDTO exportSignInfo(@PathParam("search") String search){
        return  signInfoDAO.exportSignInfo(search);
    }

    @GET
    @Path("/downloadAll/{search}")
    public ResultDTO exportAllSignInfo(@PathParam("search") String search){
        return  signInfoDAO.exportAllSignInfo(search);
    }
}

